// pages/set/res/res.js
import { base64src } from '../../../../module/utils/base.js'
const setText = (context, fs, color, x, y, c) => {
  context.setFontSize(fs);
  context.setFillStyle(color);
  context.setTextAlign('left');
  context.fillText(c, x, y);
  context.restore();
};
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		data: '',
		maskHidden: false,
		http: getApp().globalData.map,
		flightDetail: 
		    { "flt_num": "ZH9753" },
			name:''
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.setData({
			zt:options
		})
		let that = this
		wx.getStorage({
			key: 'uid',
			success: (e) => {
				console.log(e)
				this.setData({
					uid: e.data
				})
				wx.request({
					url: getApp().globalData.http + 'action=getWXACodeUnlimit&uid=' + e.data.uid + '&gid='+options.id+'&type=goods',
					success: (e) => {
						let flight = that.data.flightDetail
						let fsm = wx.getFileSystemManager(); //文件管理器
						let FILE_BASE_NAME = 'delay_certificate_'; //自定义文件名
						let buffer = wx.base64ToArrayBuffer(e.data.data.buffer); //转为Buffer数据
						const filePath = `${wx.env.USER_DATA_PATH}/${FILE_BASE_NAME}.png`; //文件路径
							fsm.writeFile({ //把图片从本来拿到
								filePath,
								data: buffer,
								encoding: 'binary',
								success() {
									that.setData({
										imgDataUrl: filePath,
									})
								},
							});
						that.setData({
							imgurl: "data:image/png;base64," + e.data.data.buffer
						})
					}
				})
			}
		})
		wx.request({
			url: getApp().globalData.http + 'action=getRegistHB',
			success: (e) => {
				console.log(e)
				this.setData({
					data: e.data.data
				})
			}
		})
	},
	//点击提交按钮
	btnclick: function() {
		var text = this.data.inputValue
		wx.showToast({
			title: text,
			icon: 'none',
			duration: 2000
		})
	},
	onShareAppMessage: function(options) {
		return {
			title: '车轻松共享养车',
			desc: '分享页面的内容',
			path: '/pages/goods/goods?id='+this.data.zt.id+'&tjr='+this.data.uid // 路径，传递参数到指定页面。
		}
	},


	//将canvas转换为图片保存到本地，然后将图片路径传给image图片的src
	createNewImg: function(e) {
		let that=this
		var ctx = wx.createCanvasContext('mycanvas');
		    var c_avatar = this.data.path;
		    var wechat = this.data.imgDataUrl;
		    // 绘制背景颜色
		    ctx.fillStyle = "#fff";
		    ctx.fillRect(0, 0, 375, 682);
		    // 绘制孩子头像、姓名  年份
		    //绘制名字
		    setText(ctx, 16, '#333333', 90, 45, that.data.uid.nick_name);
		    //绘制年龄
		    setText(ctx, 14, '#333333', 90, 65, '手机号'+that.data.uid.mobile);
		    // 绘制画报外框
		    ctx.rect(20, 85, 335, 457)
		    ctx.setFillStyle('white')
		    ctx.fill()
		    // 绘制画报背景图
		    //这个地方的图片是需要注意，图片需要下载不然，手机上不能正常显示！！！
		    ctx.drawImage(c_avatar, 30.85, 95, 315, 528);
		    //头像
		    ctx.arc(45, 45, 25, 0, 2 * Math.PI) //画出圆
		    ctx.strokeStyle = "#fff";
		    ctx.clip(); //裁剪上面的圆形
		    
		    ctx.drawImage(wechat, 20, 20, 50, 50);
		    // 绘制生成画报
		    ctx.draw(true, setTimeout(function () {
		      wx.canvasToTempFilePath({
		        canvasId: 'mycanvas',
		        success: function (res) {
		          console.log(res)
		          var tempFilePath = res.tempFilePath;
		          that.setData({
		            imagePath: tempFilePath
		          });
		        },
		        fail: function (res) {
		          console.log(res);
		        }
		      })
		    }, 1000));
	},
	//点击保存到相册
	baocun: function() {
		var that = this
		wx.saveImageToPhotosAlbum({
			filePath: that.data.imagePath,
			success(res) {
				wx.showModal({
					content: '海报已保存到相册',
					showCancel: false,
					confirmText: '确定',
					confirmColor: '#333',
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击确定');
							/* 该隐藏的隐藏 */
							that.setData({
								maskHidden: false
							})
						}
					},
					fail: function(res) {
						console.log(11111)
					}
				})
			}
		})
	},
	//点击生成
	formSubmit: function(e) {
		var that = this;
		this.setData({
			maskHidden: false
		});
		wx.showToast({
			title: '海报生成中...',
			icon: 'loading',
			duration: 1000
		});
		wx.downloadFile({
			url: e.currentTarget.dataset.id, //网络路径
			success: function(res3) {
				//背景图
				that.setData({
					path: res3.tempFilePath
				})
			}
		})
		setTimeout(function() {
			wx.hideToast()
			that.createNewImg();
			that.setData({
				maskHidden: true
			});
		}, 1000)
	},
})
